package com.intellij.ide.license.impl;

import com.intellij.ide.license.LicenseFactory;
import com.intellij.ide.license.LicenseMessages;
import com.intellij.ide.license.LicenseSource;
import com.intellij.licensecommon.data.LicenseData;

public abstract class GeneralLicenseSource implements LicenseSource {

    protected LicenseFactory licenseFactory;
    private LicenseData licenseData;

    public GeneralLicenseSource() {
    }

    public GeneralLicenseSource(LicenseFactory licenseFactory) {
        if (licenseFactory == null) {
            throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[]{
                    "0", "com/intellij/ide/license/impl/GeneralLicenseSource", "<init>"
            }));
        }
        this.licenseFactory = licenseFactory;
    }

    @Override
    public void checkValid() throws LicenseException {
        LicenseData l = getLicenseData();

        if (l == null) {
            throw new LicenseException();
        }

        try {
            this.licenseFactory.createLicenseValidator().isValid(l);
        } catch (LicenseException e) {
            setLicenseData(null);
            throw e;
        }
    }

    protected LicenseMessages getLicenseMessages() {
        return this.licenseFactory.getLicenseMessages();
    }

    @Override
    public LicenseData getLicenseData() {
        return this.licenseData;
    }

    public void setLicenseData(LicenseData licenseData) {
        this.licenseData = licenseData;
    }
}
